﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using MeizuSDK.Presentation.Integration;

namespace MeizuSDK.Presentation
{
    /// <summary>
    /// 控件集合
    /// </summary>
    public class ControlCollection : IEnumerable<Control>
    {
        #region 私有变量
        UIElement owner;
        List<Control> collection;
        #endregion

        #region 公共属性
        /// <summary>
        /// 获取集合的所有者
        /// </summary>
        public virtual UIElement Owner
        {
            get
            {
                return this.owner;
            }
        }
        /// <summary>
        /// 获取控件集合中控件的数量
        /// </summary>
        public virtual int Count
        {
            get
            {
                return collection.Count;
            }
        }
        /// <summary>
        /// 获取集合中指定索引的控件
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public virtual Control this[int index]
        {
            get
            {
                return collection[index];
            }
        }
        #endregion

        #region 构造函数
        /// <summary>
        /// 构建控件集合
        /// </summary>
        /// <param name="owner">控件集合所有者</param>
        public ControlCollection(UIElement owner)
        {
            this.owner = owner;
            collection = new List<Control>();
        }
        #endregion

        #region 函数
        /// <summary>
        /// 向集合中添加控件
        /// </summary>
        /// <param name="control">待添加的控件</param>
        public virtual void Add(Control control)
        {
            if (control == null)
            {
                throw new ArgumentNullException("control");
            }

            lock (collection)
            {
                control.Parent = owner;
                // 判断控件是否是WinForm控件承载器
                if (control is ControlHost)
                {

                }
                else
                {
                    owner.AddChild(control);
                }
                collection.Add(control);
            }
        }
        /// <summary>
        /// 向集合中添加控件集合
        /// </summary>
        /// <param name="controls">待添加的控件集合</param>
        public void AddRange(IEnumerable<Control> controls)
        {
            foreach (Control ctr in controls)
            {
                Add(ctr);
            }
        }
        /// <summary>
        /// 移除集合中指定的控件实例
        /// </summary>
        /// <param name="control">待移除的控件</param>
        public virtual void Remove(Control control)
        {
            //owner.AddChild(control);
            // Fixed 
            if (owner.IsHandleCreated)
                owner.RemoveChild(control);
            collection.Remove(control);
        }
        /// <summary>
        /// 移除集合中指定索引的控件
        /// </summary>
        /// <param name="nIndex">控件在集合中的索引</param>
        public virtual void Remove(int nIndex)
        {
            lock (collection)
            {
                if (nIndex < 0 || nIndex >= collection.Count)
                {
                    throw new IndexOutOfRangeException("控件集合中不存在索引为" + nIndex + "的控件。");
                }

                Remove(collection[nIndex]);
            }
        }
        /// <summary>
        /// 清空控件集合
        /// </summary>
        public virtual void Clear()
        {
            lock (collection)
            {
                int n = collection.Count;

                if (owner is Control)
                {
                    for (int i = 0; i < n; i++)
                    {
                        collection[i].Dispose();
                    }
                    if (owner.IsHandleCreated)
                        owner.RemoveAllChildren();
                }
                else
                {
                    for (int i = 0; i < n; i++)
                    {
                        if (owner.IsHandleCreated)
                            owner.RemoveChild(collection[i]);// 从窗口中移除控件
                        collection[i].Dispose();
                    }
                }

                collection.Clear();
            }
        }

        /// <summary>
        /// 根据控件ID获取控件实例
        /// </summary>
        /// <param name="id">控件ID</param>
        /// <returns>如果返回null，表示未找到指定Id的控件。</returns>
        public Control FindControlById(int id)
        {
            lock (collection)
            {
                int n = collection.Count;

                for (int i = 0; i < n; i++)
                {
                    Control ctr = collection[i];

                    if (ctr.Id == id)
                    {
                        return collection[i];
                    }
                    // 判断控件是否存在子控件
                    if (ctr.HasChildren)
                    {
                        // 从子控件列表中搜索指定Id
                        Control child = ctr.Controls.FindControlById(id);

                        if (child != null)
                            return child;
                    }
                }

                return null;
            }
        }
        /// <summary>
        /// 根据坐标点获取控件
        /// </summary>
        /// <param name="x">鼠标X坐标</param>
        /// <param name="y">鼠标Y坐标</param>
        /// <returns>如果返回null，表示未找到指定Id的控件。</returns>
        public Control FindControlByPoint(int x, int y)
        {
            lock (collection)
            {
                int count = collection.Count;

                for (int i = 0; i < count; i++)
                {
                    Control ctr = collection[i];

                    if (ctr.ClientRectangle.Contains(x, y))
                        return ctr;

                    if (ctr.HasChildren)
                    {
                        Control child = ctr.Controls.FindControlByPoint(x, y);

                        if (child != null)
                            return child;
                    }
                }

                return null;
            }
        }
        /// <summary>
        /// 处理非鼠标消息
        /// </summary>
        /// <param name="message"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        internal void MzDefWndProc(uint message, uint wParam, uint lParam)
        {
            lock (collection)
            {
                int n = collection.Count;

                for (int i = 0; i < n; i++)
                {
                    Control ctr = collection[i];
                    ctr.MzDefWndProc(message, wParam, lParam);

                    // 判断控件是否存在子控件
                    if (ctr.HasChildren)
                    {
                        ctr.Controls.MzDefWndProc(message, wParam, lParam);
                    }
                }
            }
        }
        #endregion

        #region IEnumerable<Control> 成员

        public IEnumerator<Control> GetEnumerator()
        {
            foreach (Control ctr in collection)
            {
                yield return ctr;
            }
        }

        #endregion

        #region IEnumerable 成员

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return collection.GetEnumerator();
        }

        #endregion
    }
}
